package com.xuexiang.xui_lib.component.button;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentState;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import com.xuexiang.xui_lib.util.AttrValue;

import java.util.Arrays;

public class SuperButton extends Button implements Component.DrawTask, Component.ComponentStateChangedListener {

    private float mPhase;                       // 虚线相关，不知道啥意思？
    private float[] mDashPath;                  // 虚线,不知道啥意思？

    private int strokeWidth;                    // 边框的宽度
    private float cornersRadius;                // 四个角的半径
    private float[] cornersRadius2;             // 分别设置四个角的半径

    private RgbColor strokeColor;               // 边框颜色
    private RgbColor normalColor;               // 选择器选中颜色
    private RgbColor selectColor;               // 选择器选中颜色
    private RgbColor[] gradientColors;          // 渐变颜色
    private RgbColor disabledColor;             // 不可点击的颜色

    private StateElement mBackground;

    //private EventHandler mHandle = new EventHandler(EventRunner.getMainEventRunner());

    private int shape = ShapeElement.RECTANGLE; // 形状

    public SuperButton(Context context) {
        this(context, null);
    }

    public SuperButton(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public SuperButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        parseAttrSet(attrSet);
        addDrawTask(this);
        if (isClickable() && isEnabled()) {
            setComponentStateChangedListener(this);
        }
    }

    private void parseAttrSet(AttrSet attrSet) {
        Color defaultColor = new Color();
        // 形状
        shape = AttrValue.get(attrSet, "super_shape", ShapeElement.RECTANGLE);
        // 默认颜色
        Color _normalColor = AttrValue.get(attrSet, "super_normal_color", defaultColor);
        if (_normalColor.getValue() != 0) {
            normalColor = RgbColor.fromArgbInt(_normalColor.getValue());
        }
        // 选中、点击按下颜色
        Color _selectColor = AttrValue.get(attrSet, "super_select_color", defaultColor);
        if (_selectColor.getValue() != 0) {
            selectColor = RgbColor.fromArgbInt(_selectColor.getValue());
        }
        // 边框宽度
        int _strokeWidth = AttrValue.get(attrSet, "super_stroke_width", 0);
        if (_strokeWidth != 0) {
            strokeWidth = AttrHelper.vp2px(_strokeWidth, getContext());
        }
        // 边框色
        Color _strokeColor = AttrValue.get(attrSet, "super_stroke_color", defaultColor);
        if (_strokeColor.getValue() != 0) {
            strokeColor = RgbColor.fromArgbInt(_strokeColor.getValue());
        }
        // 圆角
        int _cornersRadius = AttrValue.get(attrSet, "super_corners_radius", 0);
        if (_cornersRadius != 0) {
            cornersRadius = AttrHelper.vp2px(_cornersRadius, getContext());
        }
        // 四个角
        if (shape == ShapeElement.RECTANGLE) {
            int leftTop = AttrValue.get(attrSet, "super_lef_top_radius", 0);
            int leftBottom = AttrValue.get(attrSet, "super_lef_bottom_radius", 0);
            int rightTop = AttrValue.get(attrSet, "super_right_top_radius", 0);
            int rightBottom = AttrValue.get(attrSet, "super_right_bottom_radius", 0);
            cornersRadius2 = new float[]{leftTop, leftBottom, rightTop, rightBottom};
        }
        // 不可点击颜色
        Color _notClickColor = AttrValue.get(attrSet, "super_disabled_color", defaultColor);
        if (_notClickColor.getValue() != 0) {
            disabledColor = RgbColor.fromArgbInt(_notClickColor.getValue());
        }
        // 渐变色
        Color g_start_color = AttrValue.get(attrSet, "super_g_start_color", defaultColor);
        Color g_end_color = AttrValue.get(attrSet, "super_g_end_color", defaultColor);
        if (g_start_color.getValue() != 0 && g_end_color.getValue() != 0) {
            RgbColor startColor = RgbColor.fromArgbInt(g_start_color.getValue());
            RgbColor endColor = RgbColor.fromArgbInt(g_end_color.getValue());
            gradientColors = new RgbColor[]{startColor, endColor};
        }
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        init();
        setBackground(mBackground);
    }

    private void init() {
        mBackground = new StateElement();
        apply();
    }

    private void apply() {
        if (selectColor != null) {
            applyPressedElement();
        }
        if (disabledColor != null) {
            applyDisabledElement();
        }
        applyEmptyElement();
    }

    private void applyEmptyElement() {
        ShapeElement mStateEmptyElement = new ShapeElement();
        mStateEmptyElement.setShape(shape);
        // 背景色
        if (normalColor != null) {
            mStateEmptyElement.setRgbColor(normalColor);
        } else {
            normalColor = new RgbColor(255, 255, 255, 255);
            mStateEmptyElement.setRgbColor(normalColor);
        }
        // 边框宽度，边框颜色
        if (strokeWidth != 0 && strokeColor != null) {
            mStateEmptyElement.setStroke(strokeWidth, strokeColor);
        }
        // 圆角
        if (cornersRadius != 0) {
            mStateEmptyElement.setCornerRadius(cornersRadius);
        }
        if (cornersRadius2 != null) {
            mStateEmptyElement.setCornerRadiiArray(cornersRadius2);
        }
        if (gradientColors != null) {
            mStateEmptyElement.setRgbColors(gradientColors);
        }
        if (gOrientation != null) {
            mStateEmptyElement.setGradientOrientation(gOrientation);
        }
        if (mDashPath != null) {
            mStateEmptyElement.setDashPathEffectValues(mDashPath, mPhase);
        }
        mBackground.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, mStateEmptyElement);
    }


    private void applyDisabledElement() {
        ShapeElement mDisabledElement = new ShapeElement();
        mDisabledElement.setShape(shape);
        // 背景色
        mDisabledElement.setRgbColor(disabledColor);
        // 圆角
        if (cornersRadius != 0) {
            mDisabledElement.setCornerRadius(cornersRadius);
        }
        if (cornersRadius2 != null) {
            mDisabledElement.setCornerRadiiArray(cornersRadius2);
        }
        mBackground.addState(new int[]{ComponentState.COMPONENT_STATE_DISABLED}, mDisabledElement);
    }

    private void applyPressedElement() {
        ShapeElement mStatePressedElement = new ShapeElement();
        mStatePressedElement.setShape(shape);
        mStatePressedElement.setRgbColor(selectColor);
        if (cornersRadius != 0) {
            mStatePressedElement.setCornerRadius(cornersRadius);
        }
        if (cornersRadius2 != null) {
            mStatePressedElement.setCornerRadiiArray(cornersRadius2);
        }
        mBackground.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, mStatePressedElement);
    }


    /**
     * 设置形状
     *
     * @param shape
     * @return SuperButton
     */
    public SuperButton setShape(int shape) {
        this.shape = shape;
        invalidate();
        return this;
    }

    /**
     * 设置状态选择器颜色
     *
     * @param selectColor
     * @return SuperButton
     */
    public SuperButton setShapeSelectorColor(RgbColor selectColor) {
        this.selectColor = selectColor;
        invalidate();
        return this;
    }


    /**
     * 设置填充的颜色
     *
     * @param color 颜色
     * @return 对象
     */
    public SuperButton setShapeColor(RgbColor color) {
        this.normalColor = color;
        invalidate();
        return this;
    }

    /**
     * 设置边框宽度
     *
     * @param width 边框宽度值
     * @param color 设置边框颜色
     * @return return
     */
    public SuperButton setShapeStrokeWidthColor(int width, RgbColor color) {
        this.strokeWidth = AttrHelper.vp2px(width, getContext());
        this.strokeColor = color;
        invalidate();
        return this;
    }


    /**
     * 设置圆角半径
     *
     * @param radius 半径
     * @return 对象
     */
    public SuperButton setShapeCornersRadius(float radius) {
        this.cornersRadius = radius;
        invalidate();
        return this;
    }

    /**
     * 分别设置四个角半径
     *
     * @param radius
     * @return SuperButton
     */
    public SuperButton setShapeCornersRadius(float[] radius) {
        this.cornersRadius2 = Arrays.copyOf(radius,radius.length);
        invalidate();
        return this;
    }

    private ShapeElement.Orientation gOrientation;

    /**
     * 设置背景渐变方向
     *
     * @param orientation orientation
     * @return return
     */
    public SuperButton setShapeGradientOrientation(ShapeElement.Orientation orientation) {
        this.gOrientation = orientation;
        invalidate();
        return this;
    }


    /**
     * 设置渐变颜色
     *
     * @param gradientColors
     * @return SuperButton 对象
     */

    public SuperButton setShapeGradientColor(RgbColor[] gradientColors) {
        this.gradientColors = Arrays.copyOf(gradientColors,gradientColors.length);
        invalidate();
        return this;
    }

    /**
     * 设置不能点击状态的颜色
     *
     * @param color
     * @return SuperButton
     */
    public SuperButton setDisabledColor(RgbColor color) {
        this.disabledColor = color;
        invalidate();
        return this;
    }

    /**
     * new float[]{10, 21, 32, 43, 54, 65}, 1
     *
     * @param dashPath
     * @param phase
     * @return SuperButton
     */
    public SuperButton setShapeDashPathEffectValues(float[] dashPath, float phase) {
        this.mDashPath = Arrays.copyOf(dashPath,dashPath.length);
        this.mPhase = phase;
        invalidate();
        return this;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        this.setAlpha(1F);
    }

    private static final int DOWN = 18432; // 这个值是根据具体打印后得到的
    private static final int UP = 2048;

    @Override
    public void onComponentStateChanged(Component component, int state) {
        switch (state) {
            case DOWN:
                if (selectColor == null) {
                    this.setAlpha(0.7F);
                }
                break;
            case UP:
            default:
                setAlpha(1);
        }
    }
}
